package com.yolib.sanyi.pic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;

public class ImageLoader {

	private static ExecutorService executorService = Executors.newFixedThreadPool(1);
//	private static ExecutorService executorSrcService = Executors.newFixedThreadPool(2);

//	public static void getSrcBitmap(final Context context, final String id, final LoadCallback callback)
//	{
//		executorSrcService.submit(new Runnable() {
//
//			@Override
//			public void run() {
//				Bitmap mBitmap = MemoryCache.getInstance().getBitmap(id);
//				if(mBitmap == null)
//				{
//					mBitmap = FileCache.getInstance(context).getBitmap(id);
//					if(mBitmap == null)
//					{
//						//�^�ǿ�~
//					}
//					else {
//						callback.result(LoadCallback.LOAD_SUCCESS, id, mBitmap);
//					}
//				}
//				else {
//					callback.result(LoadCallback.LOAD_SUCCESS, id, mBitmap);
//				}
//			}
//		});
//	}


	public static void getPicBitmap(final Context context, final String id, final LoadCallback callback)
	{
		final Handler mHandler = new Handler();
		executorService.submit(new Runnable() {

			@Override
			public void run() {

				Bitmap mBitmap = MemoryCache.getInstance().getBitmap(id);
				if(mBitmap == null)
				{
					mBitmap = FileCache.getInstance(context).getBitmap(id);
					if(mBitmap == null)
					{
						final LoadCallback mCallback = new LoadCallback(){
							@Override
							public void result(final int status, final String id, final Object object) {

								if(status == LoadCallback.LOAD_SUCCESS)
								{
									MemoryCache.getInstance().putBitmap(id, (Bitmap) object);
									FileCache.getInstance(context).putBitmap(id, (Bitmap) object);
								}
								
								mHandler.postDelayed(new Runnable() {
									
									@Override
									public void run() {
										callback.result(status, id,  object);
									}
								}, 50);
								
							}

							@Override
							public void processing(int arg) {
								super.processing(arg);
							}
						};
						DownloadPic.getPic(id, mCallback);
					}
					else {
						final Bitmap result_bitmpp = mBitmap;
						mHandler.postDelayed(new Runnable() {
							
							@Override
							public void run() {
								callback.result(LoadCallback.LOAD_SUCCESS, id,  result_bitmpp);
							}
						}, 50);
					}
				}
				else {
					final Bitmap result_bitmpp = mBitmap;
					mHandler.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							callback.result(LoadCallback.LOAD_SUCCESS, id,  result_bitmpp);
						}
					}, 50);
				}
			}
		});
	}


	


}
